package com.bh.ledger.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.common.annotation.DataDictClass;
import com.bh.common.constant.VerifiZbStatus;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.SecurityUtils;
import com.bh.common.utils.StringUtils;
import com.bh.ledger.domain.JcVerificationRecord;
import com.bh.ledger.domain.JcVerificationZb;
import com.bh.ledger.service.IJcVerificationZbService;
import com.bh.ledger.mapper.JcVerificationZbMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 核销记录Service业务层处理
 *
 * @author slx
 * @date 2023-04-24
 */
@Service
public class JcVerificationZbServiceImpl extends ServiceImpl<JcVerificationZbMapper, JcVerificationZb> implements IJcVerificationZbService {
    @Autowired
    private JcVerificationZbMapper jcVerificationZbMapper;

    /**
     * 查询核销记录
     *
     * @param id 核销记录主键
     * @return 核销记录
     */
    @Override
    public JcVerificationZb selectJcVerificationZbById(Long id) {
        JcVerificationZb jcVerificationZb = jcVerificationZbMapper.selectById(id);
        List<JcVerificationRecord> jcVerificationRecordList = jcVerificationZbMapper.selectJcVerificationRecordByJcVerificationzbId(id);
        jcVerificationZb.setRecords(jcVerificationRecordList);
        return jcVerificationZb;
    }

    /**
     * 查询核销记录列表
     *
     * @param jcVerificationZb 核销记录
     * @return 核销记录
     */
    @DataDictClass
    @Override
    public List<JcVerificationZb> selectJcVerificationZbList(JcVerificationZb jcVerificationZb) {
        return jcVerificationZbMapper.selectJcVerificationZbList(jcVerificationZb);
    }

    @DataDictClass
    @Override
    public List<JcVerificationZb> selectIncomeList(JcVerificationZb jcVerificationZb) {
        jcVerificationZb.setType(0);
        jcVerificationZb.setJcOrderId(0L);
        return jcVerificationZbMapper.selectVerificationZbStatusList(jcVerificationZb);
    }

    @DataDictClass
    @Override
    public List<JcVerificationZb> selectCostList(JcVerificationZb jcVerificationZb) {
        jcVerificationZb.setType(1);
        jcVerificationZb.setJcShipmentId(0L);
        return jcVerificationZbMapper.selectVerificationZbStatusList(jcVerificationZb);
    }

    @DataDictClass
    @Override
    public List<JcVerificationZb> selectDispatchList(JcVerificationZb jcVerificationZb) {
        jcVerificationZb.setType(1);
        jcVerificationZb.setJcDispatchId(0L);
        return jcVerificationZbMapper.selectVerificationZbStatusList(jcVerificationZb);
    }

    @Override
    public List<JcVerificationZb> selectJcVerificationZbByJcOrderId(Long orderId) {
        return jcVerificationZbMapper.selectJcVerificationZbByJcOrderId(orderId);
    }

    @Override
    public List<JcVerificationZb> selectJcVerificationZbByShipmentId(Long shipmentId) {
        return jcVerificationZbMapper.selectJcVerificationZbByShipmentId(shipmentId);
    }

    /**
     * 新增核销记录
     *
     * @param jcVerificationZb 核销记录
     * @return 结果
     */
    @Override
    public int insertJcVerificationZb(JcVerificationZb jcVerificationZb) {
        jcVerificationZb.setCreateName(SecurityUtils.getUsername());
        jcVerificationZb.setCreateTime(DateUtils.getNowDate());
        jcVerificationZb.setStatus(VerifiZbStatus.UNACTIVE);
        return jcVerificationZbMapper.insert(jcVerificationZb);
    }

    /**
     * 修改核销记录
     *
     * @param jcVerificationZb 核销记录
     * @return 结果
     */
    @Override
    public int updateJcVerificationZb(JcVerificationZb jcVerificationZb) {
        jcVerificationZb.setModifyName(SecurityUtils.getUsername());
        jcVerificationZb.setModifyTime(DateUtils.getNowDate());
        return jcVerificationZbMapper.updateById(jcVerificationZb);
    }

    @Override
    public int insertJcVerificationRecord(JcVerificationRecord verificationRecord) {
        verificationRecord.setCreateName(SecurityUtils.getUsername());
        verificationRecord.setCreateTime(DateUtils.getNowDate());
        verificationRecord.setHxTime(DateUtils.getNowDate());
        verificationRecord.setStatus(VerifiZbStatus.ACTIVE);
        verificationRecord.setOperator(SecurityUtils.getUsername());

        JcVerificationZb verificationZb = jcVerificationZbMapper.selectById(verificationRecord.getJcVerificationzbId());
        // 计算核销金额
        verificationZb.setStatus(VerifiZbStatus.ACTIVE);
        List<JcVerificationRecord> jcVerificationRecordList = jcVerificationZbMapper.selectJcVerificationRecordByJcVerificationzbId(verificationRecord.getJcVerificationzbId());
        if (jcVerificationRecordList != null && jcVerificationRecordList.size() > 0) {
            BigDecimal yhxMoney = verificationRecord.getHxMoney();
            for (JcVerificationRecord record : jcVerificationRecordList) {
                yhxMoney = yhxMoney.add(record.getHxMoney());
            }
            verificationZb.setHxMoney(yhxMoney);
            verificationZb.setWhxMoney(verificationZb.getZmoney().subtract(yhxMoney));
            verificationZb.setHxCount(jcVerificationRecordList.size() + 1);

            verificationRecord.setZmoney(verificationZb.getZmoney());
            verificationRecord.setYhxMoney(verificationZb.getHxMoney());
            verificationRecord.setWhxMoney(verificationZb.getWhxMoney());
        } else {
            verificationZb.setHxMoney(verificationRecord.getHxMoney());
            verificationZb.setWhxMoney(verificationZb.getZmoney().subtract(verificationRecord.getHxMoney()));
            verificationZb.setHxCount(1);

            verificationRecord.setYhxMoney(verificationRecord.getHxMoney());
            verificationRecord.setZmoney(verificationZb.getZmoney());
            verificationRecord.setWhxMoney(verificationZb.getZmoney().subtract(verificationRecord.getHxMoney()));
        }
        if (verificationZb.getZmoney().subtract(verificationRecord.getYhxMoney()).compareTo(BigDecimal.ZERO) == 0) {
            verificationZb.setHxStatus(VerifiZbStatus.ACTIVE);
        } else if (verificationZb.getZmoney().subtract(verificationRecord.getYhxMoney()).compareTo(BigDecimal.ZERO) > 0) {
            verificationZb.setHxStatus(VerifiZbStatus.PART_VER);
        }
        jcVerificationZbMapper.updateById(verificationZb);
        // 保存核销记录
        return jcVerificationZbMapper.batchJcVerificationRecord(Collections.singletonList(verificationRecord));
    }

    @Override
    public int updateJcVerificationRecord(JcVerificationRecord jcVerificationRecord) {
        jcVerificationRecord.setModifyName(SecurityUtils.getUsername());
        jcVerificationRecord.setModifyTime(DateUtils.getNowDate());
        return jcVerificationZbMapper.updateJcVerificationRecord(jcVerificationRecord);
    }

    /**
     * 批量删除核销记录主
     *
     * @param ids 需要删除的核销记录主主键
     * @return 结果
     */

    @Override
    public int deleteJcVerificationZbByIds(Long[] ids)
    {
        jcVerificationZbMapper.deleteJcVerificationRecordByJcVerificationzbIds(ids);
        return jcVerificationZbMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除核销记录主信息
     *
     * @param id 核销记录主主键
     * @return 结果
     */

    @Override
    public int deleteJcVerificationZbById(Long id)
    {
        jcVerificationZbMapper.deleteJcVerificationRecordByJcVerificationzbId(id);
        return jcVerificationZbMapper.deleteById(id);
    }

    @Override
    public int deleteJcVerificationZbByOrderId(Long id) {
        AtomicInteger num = new AtomicInteger();
        List<JcVerificationZb> jcVerificationRecordList = jcVerificationZbMapper.selectJcVerificationZbByJcOrderId(id);
        jcVerificationRecordList.forEach(jcVerificationZb -> {
            jcVerificationZbMapper.deleteJcVerificationRecordByJcVerificationzbId(id);
            num.addAndGet(jcVerificationZbMapper.deleteById(jcVerificationZb.getId()));
        });
        return num.get();
    }

    /**
     * 新增核销记录详情信息
     *
     * @param jcVerificationZb 核销记录主对象
     */
    public void insertJcVerificationRecord(JcVerificationZb jcVerificationZb)
    {
        List<JcVerificationRecord> jcVerificationRecordList = jcVerificationZb.getRecords();
        Long id = jcVerificationZb.getId();
        if (StringUtils.isNotNull(jcVerificationRecordList))
        {
            List<JcVerificationRecord> list = new ArrayList<>();
            for (JcVerificationRecord jcVerificationRecord : jcVerificationRecordList)
            {
                jcVerificationRecord.setJcVerificationzbId(id);
                list.add(jcVerificationRecord);
            }
            if (list.size() > 0)
            {
                jcVerificationZbMapper.batchJcVerificationRecord(list);
            }
        }
    }
}
